package com.weimei.conversionTreasure.viewModel;

import android.app.Application;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.activity.AddJxsActivity;
import com.weimei.conversionTreasure.activity.AddKhActivity;
import com.weimei.conversionTreasure.activity.AddMarketActivity;
import com.weimei.conversionTreasure.activity.JxsDetailActivity;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.http.entity.DealerDetailBean;
import com.weimei.conversionTreasure.utils.UserUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class KhDetailVM extends TitleVM{


    public KhDetailVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("资料详情");
    }

    public ObservableField<DealerDetailBean.DataDTO> entity = new ObservableField<>();


    public BindingCommand edit = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putSerializable("data",entity.get());
        startActivity(AddKhActivity.class,bundle);
    });

    public BindingCommand add = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString("id",entity.get().getId()+"");
        startActivity(AddMarketActivity.class,bundle);
    });

    public ObservableInt empty = new ObservableInt(View.GONE);
    public ObservableList<TransactionItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<TransactionItemVM> itemBinding = ItemBinding.of(BR.viewModel,R.layout.item_trans);


    public void getData(String id){
        observableList.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("sid",id);
        model.dealer_details(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<DealerDetailBean>() {
                    @Override
                    public void onNext(DealerDetailBean detailBean) {
                        if (detailBean.issucc){
                            entity.set(detailBean.getData());
                            if (detailBean.getData().getTradeList().size() > 0){
                                empty.set(View.GONE);
                                for (DealerDetailBean.DataDTO.TradeListDTO bean:detailBean.getData().getTradeList()){
                                    TransactionItemVM itemVM = new TransactionItemVM(KhDetailVM.this,bean);
                                    observableList.add(itemVM);
                                }
                            }else {
                                empty.set(View.VISIBLE);
                            }

                        }else {
                            empty.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
